package com.traffic.between.adaptor;

import java.util.Date;
import java.util.Map;
import org.apache.log4j.Logger;
import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.GxhlSdServiceImpl;
import com.traffic.between.util.ToolUtils;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;

@ChooseProvider(channel = "sdgxhl")
public class GxhlSdAdaptor implements IAdaptor{
	private static Logger log = Logger.getLogger(GxhlSdAdaptor.class);

	
	public void order() {
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
		String productId = orderBean.getProductCode();
		String phoneNo = orderBean.getPhoneNo();
		String cstmOrderNo = orderBean.getIds();
		log.info("sdgxhl流量订购接口：productId:" + productId+",phoneNo:"+phoneNo+",cstmOrderNo:"+cstmOrderNo);
		String result = GxhlSdServiceImpl.service.order(productId, phoneNo, cstmOrderNo);
		JSONObject json = JSONObject.fromObject(result);

		String code = (String) json.get("code");
		Object data = json.get("data");

		if (("0000".equals(code)) && (!(ToolUtils.isBlank(data)))) {
		
			orderBean.setMessage(json.getString("msg"));
			@SuppressWarnings("unchecked")
			Map<String, String> map = (Map<String, String>) json.get("data");
			if ("0".equals(map.get("status"))) {
				
				orderBean.setStatus("11");
			}
			
		} else {
		
			orderBean.setStatus("8");
			orderBean.setMessage(json.getString("msg"));
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}


	public void status() {
		OrderBean orderBean = OrderInfoContext.getContext();
		
		String cstmOrderNo = orderBean.getIds();
		log.info("sdgxhl流量订购状态查询接口请求 cstmOrderNo:" + cstmOrderNo);
		String result = GxhlSdServiceImpl.service.orderStatus(cstmOrderNo);
		JSONObject json = JSONObject.fromObject(result);

		String code = (String) json.get("code");
		Object data = json.get("data");

		if (("0000".equals(code)) && (!(ToolUtils.isBlank(data)))) {
			
			orderBean.setMessage(json.getString("msg"));

			@SuppressWarnings("unchecked")
			Map<String, String> map = (Map<String, String>) json.get("data");
			if ("0".equals(map.get("status"))) {
			
				orderBean.setStatus("1");
			}
			
		} else {
			
			orderBean.setErrorMsg("订购失败");
			orderBean.setStatus("8");
			orderBean.setMessage(json.getString("msg"));
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}
}